package cn.tp.ksoft.civil_driver.module.login;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

import cn.tp.ksoft.civil_driver.DriverBaseActivity;
import cn.tp.ksoft.civil_driver.R;
import cn.tp.ksoft.civil_driver.bean.ResultBean;
import cn.tp.ksoft.civil_driver.module.MainActivity;
import cn.tp.ksoft.civil_driver.request.DriverRequest;
import cn.tp.ksoft.civil_driver.request.RequestModules;
import cn.tp.ksoft.civil_driver.utils.Constants;
import cn.tp.ksoft.civil_driver.utils.SystemPreference;
import cn.tp.ksoft.library_civil.view.AppManager;

/**
 * @author YYang 2016/3/24
 */
public class LoginedActivity extends DriverBaseActivity implements OnClickListener {
    private EditText mEdtPwd;
    private DriverRequest request;
    private final int REQUEST_VALIDATE_PHONE = 2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.driver_activity_logined);

        request = new DriverRequest(this, new WeakHandler(this));

        mEdtPwd = (EditText) findViewById(R.id.edt_pwd);
        findViewById(R.id.btn_login).setOnClickListener(this);
        findViewById(R.id.tv_login).setOnClickListener(this);
        findViewById(R.id.tv_find_pwd).setOnClickListener(this);
        findViewById(R.id.btn_eye).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_login: {
                //login
                Intent intent = new Intent(LoginedActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
                break;
            }
            case R.id.tv_find_pwd: {
                //找回密码
                Intent intent = new Intent(LoginedActivity.this, ResetPwdActivity.class);
                String phone = SystemPreference.readSystemValue(this, SystemPreference.SYSTEM_USER_LOGIN_PHONENAME);
                intent.putExtra(Constants.KEY_PHONE, phone);
                startActivity(intent);
                break;
            }

            case R.id.btn_eye : {
                int type = mEdtPwd.getInputType();
                mEdtPwd.setInputType(type == InputType.TYPE_TEXT_VARIATION_PASSWORD ?
                        InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD : InputType.TYPE_TEXT_VARIATION_PASSWORD);
                break;
            }
            case R.id.btn_login: {
                String phone = SystemPreference.readSystemValue(this, SystemPreference.SYSTEM_USER_LOGIN_PHONENAME);
                String pwd = mEdtPwd.getText().toString();
                if (TextUtils.isEmpty(phone)
                        || TextUtils.isEmpty(pwd)) {
                    Toast.makeText(LoginedActivity.this, getString(R.string.driver_tip_input_phone_pwd),
                            Toast.LENGTH_SHORT).show();
                    return;
                }

                request.isUsedDevice(phone);
                break;
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_VALIDATE_PHONE) {
            if (resultCode == RESULT_OK) {
                loginFinish();
            }
        }
    }

    private void loginFinish() {
    	// launch main activity when self is top activity
        if (AppManager.getAppManager().firstActivity() == this) {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
        }
        setResult(RESULT_OK);
        finish();
    }
    @Override
    protected void handleSuccessResult(int what, @NonNull ResultBean result) {
        switch (what) {
            case RequestModules.ISUSEDDEVICE : {
                String phone = SystemPreference.readSystemValue(this, SystemPreference.SYSTEM_USER_LOGIN_PHONENAME);
                String pwd = mEdtPwd.getText().toString();
                // 1. 返回的是常用设备
                boolean isUsedDevice = Boolean.valueOf(result.getData().toString());
                if (isUsedDevice) {
                    // 请求登陆
                    request.login(phone, pwd, null);
                } else {
                    Toast.makeText(this, getString(R.string.driver_tip_not_used_device),
                            Toast.LENGTH_SHORT).show();

                    Intent intent =new Intent(this, ResetPwdActivity.class);
                    intent.putExtra(Constants.KEY_IS_VALIDATE_PHONE, true);
                    intent.putExtra(Constants.KEY_PHONE, phone);
                    intent.putExtra(Constants.KEY_PWD, pwd);
                    startActivityForResult(intent, REQUEST_VALIDATE_PHONE);
                }

                break;
            }

            case RequestModules.LOGIN : {
                loginFinish();
                break;
            }
        }
    }


}
